package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.longcai.piaojia.R;
import com.longcai.piaojia.fragment.IntegrationGetFragment;
import com.longcai.piaojia.fragment.IntegrationPayFragment;
import com.zcx.helper.bound.BoundView;

/**
 * Created by Administrator on 2016/8/2.
 */
public class MyIntegrationActivity extends BaseActivity implements View.OnClickListener {


    @BoundView(R.id.title_back_rl) private RelativeLayout title_back_rl;

    @BoundView(R.id.integration_rg) private RadioGroup integration_rg;

    private IntegrationPayFragment integrationPayFragment;
    private IntegrationGetFragment integrationGetFragment;

    private Fragment[] fragments;

    private int prePosition = 0;
    private int index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_integration);

        integrationPayFragment = new IntegrationPayFragment();
        integrationGetFragment = new IntegrationGetFragment();

        fragments = new Fragment[]{integrationPayFragment, integrationGetFragment};

        getSupportFragmentManager().beginTransaction().add(R.id.integration_container_rl, integrationPayFragment)
                .show(integrationPayFragment).commit();


        integration_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

                switch (checkedId) {

                    case R.id.integration_pay_rb:

                        index = 0;

                        if (index != prePosition) {

                            trx.hide(fragments[prePosition]);
                            if (!fragments[index].isAdded()) {
                                trx.add(R.id.integration_container_rl, fragments[index]);
                            }
                            trx.show(fragments[index]).commit();

                            prePosition = index;

                        }

                        break;

                    case R.id.integration_get_rb:

                        index = 1;

                        if (index != prePosition) {

                            trx.hide(fragments[prePosition]);
                            if (!fragments[index].isAdded()) {
                                trx.add(R.id.integration_container_rl, fragments[index]);
                            }
                            trx.show(fragments[index]).commit();

                            prePosition = index;

                        }

                        break;

                }


            }
        });


        title_back_rl.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.title_back_rl:

                finish();

                break;


        }

    }


}
